home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9535 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: solon.com!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion problem
  5. Date: 10 Mar 1996 19:29:46 -0600
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4hvvmb$oqd@solutions.solon.com>
  10. References: <4hqedq$1mj@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4hqedq$1mj@solutions.solon.com>,
  14. John Bain <johnb@pivotal-dm.ccmail.compuserve.com> wrote:
  15.  >Hi,
  16.  >
  17.  >I thought I understood integral promotion, but the following example has
  18.  >me puzzled.
  19.  >
  20.  >--------------------
  21.  >#include <stdio.h>
  22.  >
  23.  >unsigned short s = 0xFFFF;
  24.  >
  25.  >int main(void)
  26.  >{
  27.  >    int i = ~s;
  28.  
  29. Converting an unsigned type to a signed type is implementation defined if that
  30. signed type is not large enough to represent all the values of the signed
  31. (which it may not be in this case). In the expression ~s, the s is first
  32. promoted to an int in an implementation whose int can hold all the values of an
  33. unsigned short. Otherwise it is converted to an unsigned int. If the latter is
  34. the case, upon assigning the unsigned int type to the integer i you are calling
  35. for implementation-defined behavior.
  36.  
  37.  >    printf ("%x %x\n", i, (int)~s);
  38.  
  39. The same caveat applies here, due to the cast to (int).
  40.  
  41.  >    if (i)
  42.  >        printf("i - NonZero\n");
  43.  >    else
  44.  >        printf("i - Zero\n");
  45.  >
  46.  >    if ((int)~s)
  47.  >        printf("(int)~s - NonZero\n");
  48.  >    else
  49.  >        printf("(int)~s - Zero\n");
  50.  >
  51.  >    return 0;
  52.  >}
  53.  >--------------------
  54.  >
  55.  >When compiled on an implementation with 32-bit 2's complement ints I get
  56.  >the following output:
  57.  >
  58.  >-------------------
  59.  >ffff0000 ffff0000
  60.  >i - NonZero
  61.  >(int)~s - Zero
  62.  >------------------
  63.  >
  64.  >The hex values and the result of the first "if" are the "surprising"
  65.  >results I was expecting.  However, the result of the second "if" has me
  66.  >confused.
  67.  >
  68.  >What am I missing?
  69.  
  70. A compiler that works?
  71.  
  72.  >(In case it's relevent, the implementation defines the conversion of an
  73.  >unsigned integer to a signed integer, when the unsigned value cannot be
  74.  >represented in the signed integer, as being performed by truncation of
  75.  >the most significant bits if the signed integer is shorter, and then
  76.  >interpreting the remaining bits as a 2's complement integer.)
  77.  
  78. But what is the size of short on that implementation? If unsigned shorts are
  79. 16-bits, and ints are 32-bits, then in the expression ~s, the s goes first to
  80. an int (which _can_ hold all the values of an unsigned short). The resulting
  81. int is then inverted bitwise (an operation which, incidentally, affects the
  82. _value_ in an implementation-specific way).
  83. -- 
  84.